//
//  profileViewController.m
//  iSeeColor
//
//  Created by narbeh shahnazarian on 12-10-21.
//  Copyright (c) 2012 Wyatt Gosling. All rights reserved.
////

#import "profileViewController.h"
#import <DropboxSDK/DropboxSDK.h>

@interface profileViewController() 

- (void)updateButtons;

@end

@implementation profileViewController


-(void)save:(id)sender
{
    //hide the keyboard
    [firstNameText resignFirstResponder];
    [lastNameText resignFirstResponder];
    
    //string containers to hold fir and last name
    NSString *first = [firstNameText text];
    NSString *last = [lastNameText text];
    
    //the NSUserDefaults instantiation and storing of user data.
    NSUserDefaults *theDefaults = [NSUserDefaults standardUserDefaults];
    [theDefaults setObject:first forKey:@"firstName"];
    [theDefaults setObject:last forKey:@"lastName"];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    //vreate instance of NSUserDefaults
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    //create the text holders
    NSString *firstName = [defaults objectForKey:@"firstName"];
    NSString *lastName = [defaults objectForKey:@"lastName"];
    
    //shoot the text back
    firstNameText.text = firstName;
    lastNameText.secureTextEntry = YES;
    lastNameText.text = lastName;
    
    //syncornize our container
    [defaults synchronize];
  //  NSLog(@"Setting observer for refreshView");
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateDropboxState:) name:@"refreshView" object:nil];    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    [super viewDidLoad];
    self.title = @"Link Account";
    
}

- (void)viewDidUnload {
    
    linkButton = nil;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self updateDropboxState:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)ReturnKeyButton:(id)sender{
    [sender resignFirstResponder];
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //NSLog(@"touchesBegan:withEvent:");
    [self.view endEditing:YES];
    [super touchesBegan:touches withEvent:event];
}


- (id)initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super initWithCoder:aDecoder])) {
        self.title = @"Link Account";
    }
    return self;
}

- (void)didPressLink {
    if (![[DBSession sharedSession] isLinked]) {
		[[DBSession sharedSession] linkFromController:self];
    }else {
        [[DBSession sharedSession] unlinkAll];
        [[[UIAlertView alloc]
           initWithTitle:@"Account unlinked!" message:@"Your dropbox account has been unlinked"
           delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
        [self updateDropboxState:nil];
    }
}

NSString *const EnableICloudTitle  = @"How to Setup iCloud";
NSString *const DisableICloudTitle = @"How to Turn iCloud Off";

NSString *const EnableICloudMessage  =
    @"Open the Settings app.\nSelect the iCloud menu.\nTurn ON Photostream.";
NSString *const DisableICloudMessage =
    @"Open the Settings app.\nSelect the iCloud menu.\nTurn OFF Photostream.";

- (void)didPressICloud {
    NSString *title;
    NSString *message;
    if ( [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil] == nil ) {
        title   = EnableICloudTitle;
        message = EnableICloudMessage;
    }
    else {
        title   = DisableICloudTitle;
        message = DisableICloudMessage;
    }
    [[[UIAlertView alloc]
        initWithTitle:title message:message
        delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}

#pragma mark private methods

@synthesize linkButton;
@synthesize cameraViewController;

- (void)updateDropboxState:(NSNotification *) notification {
    self.navigationItem.rightBarButtonItem.enabled = [[DBSession sharedSession] isLinked];
    
    if ([[DBSession sharedSession] isLinked]) {
        [[NSUserDefaults standardUserDefaults] setObject:@"dropbox" forKey:@"storage location"];
    }
    else{
        [[NSUserDefaults standardUserDefaults] setObject:@"local" forKey:@"storage location"];
    }
    NSString* title = [[DBSession sharedSession] isLinked] ? @"Unlink Dropbox" : @"Link Dropbox";
    [linkButton setTitle:title forState:UIControlStateNormal];
}


@end
